#!/bin/sh
#########################################################################
# Script to switch between Linux Bridge network stack and OpenvSwitch
# 
# Usage:
#   xe-switch-network-backend  <option>
#
# where <option> is either:
#   bridge      - Linux standard Bridge network stack
#   openvswitch - OpenvSwitch stack
#
#########################################################################

set -e

if [ $# -ne 1 ] ; then
	echo "Need bridge or openvswitch..."
	exit 1
fi

source /etc/xensource-inventory

new=$1
case $new in
    bridge) ;;
    vswitch|openvswitch) ;;
    *)
	echo "Mode must be \"bridge\" or \"vswitch\""
	exit 1
esac

if [ X"$new" = Xvswitch ] ; then
    new="openvswitch"
fi

curr=$(cat /etc/xensource/network.conf)
if [ X"$curr" = Xvswitch ] ; then
    new="openvswitch"
fi

BLACKLIST=/etc/modprobe.d/blacklist-bridge.conf
if [ "$new" = "openvswitch" ] ; then
	# Add blacklist of bridge module so it can't be loaded.
	echo "install bridge /bin/true" > $BLACKLIST
elif [ -e $BLACKLIST ] ; then
	# Remove blacklist of bridge.
	rm $BLACKLIST
fi

if [ "$new" = "openvswitch" ] ; then
	echo "Enabling openvswitch daemon"
	systemctl enable openvswitch.service openvswitch-xapi-sync.service
else
	echo "Disabling openvswitch daemon"
	systemctl disable openvswitch.service openvswitch-xapi-sync.service
fi

echo "Configure system for $new networking"
echo $new > /etc/xensource/network.conf

echo "You *MUST* now reboot your system" 1>&2
